//
//  HNSalesManCycleDetailVC.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/6.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNSalesManCycleDetailVC.h"
#import "HNSalesManCycleDetailView.h"
#import "HNSalesManCycleDetailModel.h"
#import "ZYResourceCircleController.h"
#import "HNSalesManCycleDetailCellTableViewCell.h"

@interface HNSalesManCycleDetailVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *Activity;
@property (weak, nonatomic) IBOutlet UIView *HidenView;
/** 表单 */
@property (weak, nonatomic) IBOutlet UITableView *TableView;
/** 上部分视图 */
@property (nonatomic, strong) HNSalesManCycleDetailView *HeaderView;
/** 对象 */
@property (nonatomic, strong) HNSalesManCycleDetailModel *Model;

@end

@implementation HNSalesManCycleDetailVC
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"资源圈详情";
    
    self.TableView.tableHeaderView = [UIView new];
    self.TableView.tableFooterView = [UIView new];
    [self.TableView registerNib:[UINib nibWithNibName:[HNSalesManCycleDetailCellTableViewCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    
    [self RefreshDataWithBeginTime:@"" EndTime:@""];
}


#pragma mark - 网络
- (void)RefreshDataWithBeginTime:(NSString *)BeginTime EndTime:(NSString *)EndTime {
    
    NSDictionary *Params = @{
                             @"action"      :@"getResourceByUser",
                             @"startTime"   :BeginTime,
                             @"endTime"     :EndTime,
                             @"resourceId"  :self.ResourceId
                             };
    [NetManager Get:LLBaseURL(@"resource.do") Auth:YES Params:Params Success:^(id Obj) {
        
        [self.Activity stopAnimating];
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            
            [SVProgressHUD dismiss];
            _HidenView.hidden = YES;
            self.Model = [[HNSalesManCycleDetailModel alloc] init_ValuForKeyWithDict:Obj[@"t"]];
            self.TableView.delegate = self;
            self.TableView.dataSource = self;
            self.TableView.tableHeaderView = self.HeaderView;
            self.HeaderView.Model = self.Model;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.TableView reloadData];
            });
        } else {
            
            [SVProgressHUD showErrorWithStatus:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        
        [self.Activity stopAnimating];
        [SVProgressHUD showErrorWithStatus:NetErr];
    }];
}

#pragma mark - tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.Model.orgs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    HNSalesManCycleDetailCellTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Model = self.Model.orgs[indexPath.row];
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HNSalesManCycleDetailOrgModel *Model = self.Model.orgs[indexPath.row];
    [SVProgressHUD showSuccessWithStatus:Model.websiteName];
    
    ZYResourceCircleController *vc = [ZYResourceCircleController new];
    vc.oid = [Model.OID stringValue];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44.f;
}



#pragma mark - setter
- (HNSalesManCycleDetailView *)HeaderView {
    
    if (!_HeaderView) {
     
        _HeaderView = [HNSalesManCycleDetailView ConfigCycleDetailHeaderViewWithComplete:^(NSInteger Tag) {
           
            // 开始与结束的点击事件
        }];
//        _HeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 690);
                _HeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 950 );
        // 时间选择
        __weak typeof(self)WeakSelf = self;
        _HeaderView.RefreshDataForTime = ^(NSString *BeginTime, NSString *EndTime) {
            // 发起请求
            [SVProgressHUD showInfoWithStatus:@"查询中,请稍候"];
            [WeakSelf RefreshDataWithBeginTime:BeginTime EndTime:EndTime];
        };
    }
    return _HeaderView;
}
@end
